player_cash= {}
player_model= {}
player_spawn = {}
function save_data(ply)
local steam_id=string.Explode(":",ply:SteamID())
steam_id=steam_id[3]	
local model=tostring(player_model[ply:UserID()])
if (player_cash[ply:UserID()]==nil) then 
file.Write("base_combat/accounts/id"..steam_id..".txt","1000,");
print(player_cash[ply:UserID()])
else
file.Write("base_combat/accounts/id"..steam_id..".txt",player_cash[ply:UserID()]..",");
end
file.Append("base_combat/accounts/id"..steam_id..".txt",player_model[ply:UserID()]..",");
end 

function load_data(ply)
print("[SERVER] LOADED PLAYER DATA for.."..ply:Nick())
	local steam_id =string.Explode(":",ply:SteamID())
	steam_id=steam_id[3]
		if file.Exists("base_combat/accounts/id".. steam_id ..".txt" ) then
		
		local usrInfo = file.Read( "base_combat/accounts/id" .. steam_id .. ".txt" )
		
		local i = string.find( usrInfo, "," )
		local cash = tonumber( string.sub( usrInfo, 1, i - 1 ))
		usrInfo = string.sub( usrInfo, i + 1 )
		if cash==nil then 
		cash=1000
		end
		local i = string.find( usrInfo, "," )
		local model =string.sub( usrInfo, 1, i - 1 )
		table.insert(player_model,ply:UserID(),model)
		table.insert(player_cash,ply:UserID(),cash)
		umsg.Start("set_cash", ply);
		umsg.Long(player_cash[ply:UserID()]);
		umsg.End();
	else
		
		ply:PrintMessage( HUD_PRINTTALK, "Welcome new player! You have been given $1000 to start with. Have fun!" )
		ply:PrintMessage( HUD_PRINTTALK, "Your ID is: " ..steam_id)
		ply:PrintMessage( HUD_PRINTTALK, "Press F1 to set your model any time :D")
		table.insert(player_cash,ply:UserID(),1000);
		umsg.Start("set_model", ply);
		umsg.End();		
		
		umsg.Start("set_cash", ply);
		umsg.Long(player_cash[ply:UserID()]);
		umsg.End();
		save_data(ply);
	end
end

for k,ply in pairs(player.GetAll()) do
load_data(ply);
end

		
		function GM:PlayerInitialSpawn(ply)
		load_data(ply);
		end
		
function GM:PlayerSpawn(ply)
	if (!player_model[ply:UserID()]) then 
	umsg.Start("set_model", ply);
	umsg.End();		
	end
	ply:SetModel(player_model[ply:UserID()])
	ply:Give( "weapon_physcannon" )
	ply:Give( "weapon_crowbar" )
	ply:Give( "weapon_physgun" )
	ply:SetWalkSpeed(300)
	ply:SetRunSpeed(450)
	ply:GetMaxSpeed(650)
	ply:SetArmor(100)
	ply:SetHealth(1000)
		if (player_cash[ply:UserID()]==nil) then 
		load_data(ply)
		ply:PrintMessage( HUD_PRINTTALK, "THERE WAS AN ERROR WE HAD TO RELOAD YOUR CASH FORM THE SERVER SORRY")
		end
		
	print(ply:UserID())
	print(player_cash[ply:UserID()])
	save_data(ply);
umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
umsg.End();

	if (player_spawn[ply:UserID()]) then 
	ply:SetPos(player_spawn[ply:UserID()])
	end 
end 
		
function GM:PlayerDeath(ply,Inflictor, Attacker)
		for k,v in pairs(ply:GetWeapons()) do
		ply:DropWeapon(v)
		end
end
	

function pay_day()
umsg.Start("chat_pay");
umsg.String("Pay Dy! You got $100")
umsg.End();

for k,ply in pairs(player.GetAll()) do
	local steam_id =string.Explode(":",ply:SteamID())
	steam_id=steam_id[3]
	
	table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]+100)
	save_data(ply);
	
	umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
	umsg.End();	
		end
end	
		
timer.Create("timer_payday",60*3,0,function () 
pay_day()
end)
	
function GM:ShowHelp(ply)
umsg.Start("set_model", ply);
umsg.End();	
end

function  GM:ShowTeam(ply)
pay_day();
end

function GM:ShowSpare1(ply)
	if player_cash[ply:UserID()]>=50 then 
	table.insert(player_cash,ply:UserID(),player_cash[ply:UserID()]-50)
	umsg.Start("set_cash", ply);
	umsg.Long(player_cash[ply:UserID()]);
	umsg.End();
	umsg.Start("chat_buy");
	umsg.String("You set your spawn pose for $50")
	umsg.End();
	table.insert(player_spawn,ply:UserID(),ply:GetPos())
	else 
	umsg.Start("chat_buy");
	umsg.String("You need $50 to set your spawn")
	umsg.End();
	end
end


datastream.Hook("set_model", function ( ply, handle, id, encoded, decoded )
ply:SetModel(decoded.model)
table.insert(player_model,ply:UserID(),decoded.model)
save_data(ply)
end)